package com.zkingsoft.rhi.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkingsoft.rhi.entity.ImmovableHistoricalRelics;
import com.zkingsoft.rhi.entity.ImmovableHistoricalRelicsPublication;
import com.zkingsoft.rhi.handler.Result;
import com.zkingsoft.rhi.mapper.ImmovableHistoricalRelicsPublicationMapper;
import com.zkingsoft.rhi.service.ImmovableHistoricalRelicsPublicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xieguo
 * @since 2020-11-17
 */
@Service
public class ImmovableHistoricalRelicsPublicationServiceImpl extends ServiceImpl<ImmovableHistoricalRelicsPublicationMapper, ImmovableHistoricalRelicsPublication> implements ImmovableHistoricalRelicsPublicationService {

    @Autowired
    ImmovableHistoricalRelicsPublicationMapper ihrpm;

    @Override
    public int handleList(List<ImmovableHistoricalRelics> list) {
        ArrayList<ImmovableHistoricalRelicsPublication> readIhrp = new ArrayList<>();
        list.forEach(data->{
            readIhrp.add(new ImmovableHistoricalRelicsPublication(data.getLevel(),
                    data.getPublicationBatch(),
                    data.getPublicationNumber(),
                    data.getPublicationTime()));
        });

        List<ImmovableHistoricalRelicsPublication> ihrpList = readIhrp.stream().distinct().collect(Collectors.toList());

        // 获取集合对应的迭代器，用来遍历集合中的元素的。
        ihrpList.removeIf(next -> null==next.getPublicationNumber());

        //获取数据库的数据
        List<ImmovableHistoricalRelicsPublication> relicsList = ihrpm.selectList(null);

        //文件数据重复数据的集合
        List<ImmovableHistoricalRelicsPublication> repeatData = new ArrayList<>();

        //文件重复数据更新的集合
        List<ImmovableHistoricalRelicsPublication> updateData = new ArrayList<>();

        //俩重循环筛选重复数据
        for (ImmovableHistoricalRelicsPublication publication : ihrpList) {

            for (ImmovableHistoricalRelicsPublication relicsPublication : relicsList) {
                String number = publication.getPublicationNumber();
                String sqlNumber = relicsPublication.getPublicationNumber();
                if (number.equals(sqlNumber)) {
                    repeatData.add(publication);
                    updateData.add(relicsPublication);
                }
            }
        }

//        System.out.println(repeatData.size()+"==========="+updateData.size());

        //把录入的数据放入带有id的数据里面
        for (int i = 0; i <updateData.size() ; i++) {
            updateData.get(i).setLevel(repeatData.get(i).getLevel());
            updateData.get(i).setPublicationBatch(repeatData.get(i).getPublicationBatch());
            updateData.get(i).setPublicationNumber(repeatData.get(i).getPublicationNumber());
            updateData.get(i).setPublicationTime(repeatData.get(i).getPublicationTime());
        }
//        System.out.println("repeatData=======================>"+repeatData.toString());
//        System.out.println("updateData=======================>"+updateData.toString());
        int count=0;
        if (!updateData.isEmpty()){
           count= ihrpm.batchUpdate(updateData);
        }

//        System.out.println("updateData===================>"+updateData.toString());
//        System.out.println("updateDataSize===================>"+updateData.size());

        ihrpList.removeAll(repeatData);

        if (ObjectUtil.isNotEmpty(ihrpList)){
            return ihrpm.batchInsert(ihrpList);
        }

        return count;

    }
}
